﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Site.Models;
using RedSand.Web.Mvc;
using RedSand.Site.Controllers;

namespace Site.Controllers
{
    public class FaqController : BaseController
    {
        public ActionResult Manager()
        {
            return View(new ManagerModel { Title = Translate("FaqManager", "FAQ"), AttachTinyMce = true });
        }
        [HttpGet]
        public ActionResult ListFaq(FaqSearchModel searchModel,int? page)
        {
            int Total = 0;
            ViewBag.Title = Translate("Faq", "FAQ");
            ViewBag.DescriptionMeta = Translate("FaqDescriptionMeta", "FAQ");
            ViewBag.KeywordMeta = Translate("FaqDescriptionMeta","FAQ");
            searchModel.Language = LanguageCode;
            List<FaqInfo> list = RsDataManager.GetList<FaqInfo>("FaqSearch", searchModel, page ?? 1, 35, ref Total);
            ViewBag.TotalRecord = Total;
            ViewBag.RouteValues = searchModel;
            return PartialView(list);
        }
        [HttpGet]
        public ActionResult Details(int? Id)
        {
            var info = RsDataManager.GetInfo<FaqInfo>("FAQGetInfo", "FaqID", Id) ?? new FaqInfo();
            ViewBag.Title = Translate("Faq", "FAQ");
            ViewBag.TitleName = info.Title;
            ViewBag.DescriptionMeta = info.Content;
            ViewBag.KeywordMeta = info.Title;
            return View(info);
        }
        [AjaxOrChildAction]
        public ActionResult List(FaqSearchModel searchModel, int? page)
        {
            int Total = 0;
            searchModel.Language = LanguageCode;
            List<FaqInfo> list = RsDataManager.GetList<FaqInfo>("FaqSearch", searchModel, page ?? 1, 10, ref Total);
            ViewBag.TotalRecord = Total;
            ViewBag.RouteValues = searchModel;
            return PartialView(list);
        }

        [HttpGet, AjaxOrChildAction]
        public ActionResult Create(int count)
        {
            return PartialView(new FaqInfo { Order = count + 1 });
        }

        [HttpPost, AjaxOrChildAction]
        public ActionResult Create(FaqInfo info)
        {
            if (string.IsNullOrWhiteSpace(info.Link + info.Content))
            {
                return AjaxMessage(Translate("LinkOrContentEmpty", "FAQ"));
            }
            if (ModelState.IsValid)
            {
                info.Language = LanguageCode;
                info.Content = HtmlDecode(info.Content);
                if (RsDataManager.Save("FaqSave", info))
                {
                    AddMessage(Message.CreateSuccess);
                    return RedirectToAction("List");
                }
                return AjaxMessage(Message.CreateError);
            }
            return AjaxModelError();
        }


        [HttpGet, AjaxOrChildAction]
        public ActionResult Edit(int id, int count)
        {
            ViewBag.ChildCount = count;
            return PartialView(RsDataManager.GetInfo<FaqInfo>("FaqGetInfo", "FaqID", id));
        }

        [HttpPost, AjaxOrChildAction]
        public ActionResult Edit(FaqInfo info)
        {
            if (string.IsNullOrWhiteSpace(info.Link + info.Content))
            {
                return AjaxMessage(Translate("LinkOrContentEmpty", "FAQ"));
            }
            if (ModelState.IsValid)
            {
                info.Content = HtmlDecode(info.Content);
                if (RsDataManager.Save("FAQSave", info))
                {
                    AddMessage(Message.UpdateSuccess);
                    return RedirectToAction("List");
                }
                return AjaxMessage(Message.UpdateError);
            }
            return AjaxModelError();
        }

        [AjaxOrChildAction]
        public ActionResult Delete(int[] id)
        {
            if (id != null)
            {
                int count = id.Count();
                foreach (int i in id)
                {
                    if (RsDataManager.Delete("FaqDelete", "FaqID", i))
                    {
                        count--;
                    }
                }
                if (count > 0)
                {
                    AddMessage(string.Format(Translate("CannotDeleteAnyRecords", "Message"), count));
                }
                else
                {
                    AddMessage(Message.DeleteSuccess);
                }
            }
            return RedirectToAction("List");
        }

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetTopFAQ()
        {
            List<FaqInfo> lst = RsDataManager.GetList<FaqInfo>("FAQGetList", new { language = RedSand.Web.Language.RsLanguage.LanguageCode });
            if (lst != null && lst.Count > 11)
            {
                lst = lst.Take(11).ToList();
            }
            return PartialView(lst);
        }
    }
}
